/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StudentBox.java
 *
 * Created on 8-okt-2009, 14:50:40
 */
package xmlopdracht2;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Sander Theeuwes
 */
public class StudentBox extends javax.swing.JFrame
{

  private Student student;
  private XMLOpdracht2View view;

  /** Creates new form StudentBox */
  public StudentBox(XMLOpdracht2View view)
  {
    this.view = view;
    initComponents();
    this.setTitle("Student Toevoegen");
    this.tfStudentNr.setEnabled(true);
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    this.tfStudentNr.setText("S1012472");
    this.tfVoornaam.setText("Gerben");
    this.tfTussenvoegsel.setText("");
    this.tfAchternaam.setText("Gernaat");
    this.tfStraatnaam.setText("Oostercluft");
    this.tfHuisNr.setText("38");
    this.tfPostcode.setText("8332DG");
    this.tfPlaats.setText("Steenwijk");
    this.tfLand.setText("Nederland");
    this.tfGeboortedatum.setText("12-10-1988");
    this.tfBankrekeningNr.setText("322049601");
    this.tfKlasCode.setText("M4b");

  }

  public StudentBox(XMLOpdracht2View view, Student student)
  {
    this(view);
    this.setTitle("Student Wijzigen");
    this.student = student;
    
    this.tfStudentNr.setText(student.getStudentNr());
    this.tfStudentNr.setEnabled(false);
    this.tfVoornaam.setText(student.getVoornaam());
    this.tfTussenvoegsel.setText(student.getTussenvoegsel());
    this.tfAchternaam.setText(student.getAchternaam());
    this.tfStraatnaam.setText(student.getStraatnaam());
    this.tfHuisNr.setText(student.getHuisNr());
    this.tfPostcode.setText(student.getPostcode());
    this.tfPlaats.setText(student.getPlaats());
    this.tfLand.setText(student.getLand());
    this.tfGeboortedatum.setText(student.getGeboorteDatum());
    this.tfBankrekeningNr.setText("" + student.getBankrekeningNr());
    this.tfKlasCode.setText(student.getKlascode());
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    btnOk = new javax.swing.JButton();
    lbKlasCode = new javax.swing.JLabel();
    lbBankrekeningNr = new javax.swing.JLabel();
    lbGeboortedatum = new javax.swing.JLabel();
    tfStudentNr = new javax.swing.JTextField();
    lbVoornaam = new javax.swing.JLabel();
    lbStudentNr = new javax.swing.JLabel();
    tfTussenvoegsel = new javax.swing.JTextField();
    tfVoornaam = new javax.swing.JTextField();
    lbTussenvoegsel = new javax.swing.JLabel();
    tfHuisNr = new javax.swing.JTextField();
    tfStraatnaam = new javax.swing.JTextField();
    tfAchternaam = new javax.swing.JTextField();
    lbAchternaam = new javax.swing.JLabel();
    btnCancel = new javax.swing.JButton();
    tfKlasCode = new javax.swing.JTextField();
    tfBankrekeningNr = new javax.swing.JTextField();
    tfPlaats = new javax.swing.JTextField();
    tfPostcode = new javax.swing.JTextField();
    tfGeboortedatum = new javax.swing.JTextField();
    tfLand = new javax.swing.JTextField();
    lbHuisNr = new javax.swing.JLabel();
    lbPostcode = new javax.swing.JLabel();
    lbPlaats = new javax.swing.JLabel();
    lbLand = new javax.swing.JLabel();
    lbStraatnaam = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    btnOk.setLabel("Ok");
    btnOk.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        btnOk_MouseClicked(evt);
      }
    });

    lbKlasCode.setText("Klas Code:");

    lbBankrekeningNr.setText("Bankrekening Nummer:");

    lbGeboortedatum.setText("Land:");

    lbVoornaam.setText("Voornaam:");

    lbStudentNr.setText("Student Nummer:");

    lbTussenvoegsel.setText("Tussenvoegsel:");

    lbAchternaam.setText("Achternaam:");

    btnCancel.setLabel("Cancel");
    btnCancel.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        btnCancel_MouseClick(evt);
      }
    });

    lbHuisNr.setText("Huis Nummer:");

    lbPostcode.setText("Postcode:");

    lbPlaats.setText("Plaats:");

    lbLand.setText("Geboortedatum:");

    lbStraatnaam.setText("Straatnaam:");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(lbStudentNr)
              .addComponent(lbVoornaam)
              .addComponent(lbTussenvoegsel)
              .addComponent(lbAchternaam)
              .addComponent(lbStraatnaam)
              .addComponent(lbKlasCode)
              .addComponent(lbBankrekeningNr)
              .addComponent(lbLand)
              .addComponent(lbGeboortedatum)
              .addComponent(lbPlaats)
              .addComponent(lbPostcode)
              .addComponent(lbHuisNr))
            .addGap(36, 36, 36)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
              .addComponent(tfKlasCode)
              .addComponent(tfBankrekeningNr)
              .addComponent(tfGeboortedatum)
              .addComponent(tfLand)
              .addComponent(tfPlaats)
              .addComponent(tfPostcode)
              .addComponent(tfHuisNr)
              .addComponent(tfStraatnaam)
              .addComponent(tfAchternaam)
              .addComponent(tfTussenvoegsel)
              .addComponent(tfVoornaam)
              .addComponent(tfStudentNr, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)))
          .addGroup(layout.createSequentialGroup()
            .addComponent(btnOk)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnCancel)))
        .addContainerGap(103, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfStudentNr)
          .addComponent(lbStudentNr))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfVoornaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbVoornaam))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfTussenvoegsel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbTussenvoegsel))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfAchternaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbAchternaam))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfStraatnaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbStraatnaam))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfHuisNr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbHuisNr))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbPostcode))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfPlaats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbPlaats))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfLand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbGeboortedatum))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfGeboortedatum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbLand))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfBankrekeningNr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbBankrekeningNr))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(tfKlasCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lbKlasCode))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(btnOk)
          .addComponent(btnCancel))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void btnOk_MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btnOk_MouseClicked
    {//GEN-HEADEREND:event_btnOk_MouseClicked
      // TODO add your handling code here:
      Boolean newStudent = true;
      Boolean done = false;
      if (student != null)
      {
        newStudent = false;
      }
      student = new Student(tfStudentNr.getText(),
              tfVoornaam.getText(),
              tfTussenvoegsel.getText(),
              tfAchternaam.getText(),
              tfStraatnaam.getText(),
              tfHuisNr.getText(),
              tfPostcode.getText(),
              tfPlaats.getText(),
              tfLand.getText(),
              tfGeboortedatum.getText(),
              Integer.parseInt(tfBankrekeningNr.getText()),
              tfKlasCode.getText());
      if (newStudent)
      {
        try
        {
          JFileChooser fileChooser = new JFileChooser(new File("./"));
          fileChooser.setDialogTitle("Kies een XML bestand");
          fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
          fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
          fileChooser.addChoosableFileFilter(new SimpleFileFilter("XML - Bestanden", "*.xml", true));
          fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
          int returnValue = fileChooser.showOpenDialog(this);
          if (returnValue == JFileChooser.APPROVE_OPTION)
          {
            //fileChooser.
            File saveFile = fileChooser.getSelectedFile();
            OutputStream os;
            
            if(saveFile.getName().endsWith(".xml"))
              os = new FileOutputStream(saveFile);
            else
            {
              String fileName = saveFile.getAbsolutePath() + ".xml";
              File f = new File(fileName);
              if(!f.exists())
                f.createNewFile();

              os = new FileOutputStream(f);
            }

            if(view.getXml().addStudent(student, os))
            {
              JOptionPane.showMessageDialog(null, "De student is toegevoegd", "Gelukt", JOptionPane.INFORMATION_MESSAGE);
              done = true;
            }
            else
            {
              JOptionPane.showMessageDialog(null, "Student toegevoegen is mislukt!", "Mislukt", JOptionPane.ERROR_MESSAGE);
            }
          }

        }
        catch (Exception e)
        {
          JOptionPane.showMessageDialog(null, e.getMessage(), "Gelukt", JOptionPane.INFORMATION_MESSAGE);
        }
      }
      else
      {
        //code voor het wijzigen van een student
        if(this.view.getXml().changeStudent(student, view.getSelectedStudent()))
        {
          JOptionPane.showMessageDialog(null, "De student is gewijzigd", "Gelukt", JOptionPane.INFORMATION_MESSAGE);
          done = true;
        }
      }

      //scherm sluiten
      if (done)
      {
        this.dispose();
      }
}//GEN-LAST:event_btnOk_MouseClicked

    private void btnCancel_MouseClick(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btnCancel_MouseClick
    {//GEN-HEADEREND:event_btnCancel_MouseClick
      // TODO add your handling code here:
      this.dispose();
}//GEN-LAST:event_btnCancel_MouseClick
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btnCancel;
  private javax.swing.JButton btnOk;
  private javax.swing.JLabel lbAchternaam;
  private javax.swing.JLabel lbBankrekeningNr;
  private javax.swing.JLabel lbGeboortedatum;
  private javax.swing.JLabel lbHuisNr;
  private javax.swing.JLabel lbKlasCode;
  private javax.swing.JLabel lbLand;
  private javax.swing.JLabel lbPlaats;
  private javax.swing.JLabel lbPostcode;
  private javax.swing.JLabel lbStraatnaam;
  private javax.swing.JLabel lbStudentNr;
  private javax.swing.JLabel lbTussenvoegsel;
  private javax.swing.JLabel lbVoornaam;
  private javax.swing.JTextField tfAchternaam;
  private javax.swing.JTextField tfBankrekeningNr;
  private javax.swing.JTextField tfGeboortedatum;
  private javax.swing.JTextField tfHuisNr;
  private javax.swing.JTextField tfKlasCode;
  private javax.swing.JTextField tfLand;
  private javax.swing.JTextField tfPlaats;
  private javax.swing.JTextField tfPostcode;
  private javax.swing.JTextField tfStraatnaam;
  private javax.swing.JTextField tfStudentNr;
  private javax.swing.JTextField tfTussenvoegsel;
  private javax.swing.JTextField tfVoornaam;
  // End of variables declaration//GEN-END:variables
}
